package wdt_qf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import wdt_qf.dao.*;
import wdt_qf.entity.ZxyNeedauditingretailsbillid;
import wdt_qf.entity.qf.Retailbill;
import wdt_qf.entity.qf.Retailbillindex;
import wdt_qf.entity.qf.Retailpayinfo;
import wdt_qf.entity.qf.common.ResultData;
import wdt_qf.entity.qf.pro.XF_RetailPostPro;
import wdt_qf.entity.qf.query.Gp_BillBatchOrderQuery;
import wdt_qf.entity.qf.result.PtypeInfoRes;
import wdt_qf.entity.qf.result.QfPosGoodsStockQtyRes;
import wdt_qf.entity.wdt.WdtSaleBackBillRes;
import wdt_qf.entity.wdt.WdtSaleBillRes;
import wdt_qf.service.QfRetailsBillService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class QfRetailsBillServiceImpl implements QfRetailsBillService {
    @Resource
    private RetailsBillDao retailsBillDao;
    @Resource
    private RetailbillindexDao retailbillindexDao;
    @Resource
    private RetailbillDao retailBillDao;
    @Resource
    private RetailpayinfoDao retailpayinfoDao;
    @Resource
    private StocksDao stocksDao;
    @Resource
    private ZxyNeedauditingretailsbillidDao zxyNeedauditingretailsbillidDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultData addRetailsBillToQianFang(WdtSaleBillRes wdtSaleBillRes) {
        int erec = 0;
        try {
            for (WdtSaleBillRes.ResponseBean.StockoutListBean wdtSaleBill : wdtSaleBillRes.getResponse().getStockoutList()) {
                boolean isGo = true;//是否继续执行
                if (wdtSaleBill.getTradeStatus() != 110) {
                    isGo = false;
                }

                int billIndexCountByBillCode = retailbillindexDao.getBillIndexCountByBillCode(wdtSaleBill.getSrcOrderNo());
                if (billIndexCountByBillCode > 0) {
                    log.info(wdtSaleBill.getSrcOrderNo() + "单据编号已存在,不需要在插入到千方中...");
                    continue;
                }
                //如果上面检测模块 通过,进行插入数据,(数据插入到中间库记录表,和零售单主表及明细表)
                if (isGo) {
                    Retailbillindex retailbillindex = new Retailbillindex();
                    retailbillindex.setBillDate(wdtSaleBill.getConsignTime()).setBillCode(wdtSaleBill.getSrcOrderNo()).setBillType(151).setComment("旺店通").setERec(erec).setKRec(4).setPosid(1).setTotalmoney(Double.parseDouble(wdtSaleBill.getGoodsTotalAmount())).setTotalqty(0).setDiscountMoney(0).setCash(0).setChange(0).setExplain("").setDraft(0).setSaleEtypeid(erec).setBuyerName("");
                    retailbillindexDao.insertSelective(retailbillindex);
                    int billId = retailbillindex.getBillID();
                    //如果插入的主单据返回的billId》0证明数据插入成功,可以插入明细数据
                    if (billId > 0) {
                        List<Retailbill> retailBills = this.createRetailBill(wdtSaleBill.getDetailsList(), billId, wdtSaleBill.getOrderNo(),4);
                        if (CollUtil.isEmpty(retailBills)) {
                            //TODO 手动回滚事物
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
                        for (Retailbill retailbill : retailBills) {
                            retailBillDao.insertSelective(retailbill);
                        }
                        //TODO 插入明细以后,调用修改语句,将总单据的数量改成名字单据的汇总
                        retailbillindexDao.updateBillTotalQty(billId);
                        //TODO 插入资金账户(这里要去取)
                        //根据id 获取当前单据总结
                        double billTotalMoneyByBillId = retailbillindexDao.getBillTotalMoneyByBillId(billId);
                        Retailpayinfo retailpayinfo = new Retailpayinfo();
                        retailpayinfo.setArec(70).setBillid(billId).setOrd(0).
                                setTotal(billTotalMoneyByBillId);
                        retailpayinfoDao.insertSelective(retailpayinfo);
                        //上面生成零售单没有问题,执行将单号插入到过账表中,后面将进行自动过账
                        ZxyNeedauditingretailsbillid zxyNeedauditingretailsbillid = new ZxyNeedauditingretailsbillid();
                        zxyNeedauditingretailsbillid.setRetailBillId(billId);
                        int i = zxyNeedauditingretailsbillidDao.insertSelective(zxyNeedauditingretailsbillid);
                        log.info("插入过账表成功");
                    } else {
                        log.error("零售主单据插入失败" + JSON.toJSONString(retailbillindex));
                        //TODO 手动回滚事物
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                } else {
                    log.error(wdtSaleBill.getOrderNo() + "单据状态为" + wdtSaleBill.getTradeStatus() + "不能执行插入到千方数据库中");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return null;
    }


    public ResultData addRetailsBillToQianFang2(WdtSaleBillRes wdtSaleBillRes) {
        int erec = 0;
        try {
            for (WdtSaleBillRes.ResponseBean.StockoutListBean wdtSaleBill : wdtSaleBillRes.getResponse().getStockoutList()) {
                boolean isGo = true;//是否继续执行
                if (wdtSaleBill.getTradeStatus() != 110) {
                    isGo = false;
                }

                int billIndexCountByBillCode = retailbillindexDao.getBillIndexCountByBillCode(wdtSaleBill.getSrcOrderNo());
                if (billIndexCountByBillCode > 0) {
                    log.info(wdtSaleBill.getSrcOrderNo() + "单据编号已存在,不需要在插入到千方中...");
                    continue;
                }
                //如果上面检测模块 通过,进行插入数据,(数据插入到中间库记录表,和零售单主表及明细表)
                if (isGo) {
                    Retailbillindex retailbillindex = new Retailbillindex();
                    retailbillindex.setBillDate(wdtSaleBill.getConsignTime())
                            .setBillCode(wdtSaleBill.getSrcOrderNo())
                            .setBillType(151).setComment("旺店通")
                            .setERec(erec)
                            .setKRec(8)
                            .setPosid(2)
                            .setTotalmoney(Double.parseDouble(wdtSaleBill.getGoodsTotalAmount()))
                            .setTotalqty(0).setDiscountMoney(0).setCash(0)
                            .setChange(0).setExplain("")
                            .setDraft(0)
                            .setSaleEtypeid(erec).setBuyerName("");
                    retailbillindexDao.insertSelective(retailbillindex);
                    int billId = retailbillindex.getBillID();
                    //如果插入的主单据返回的billId》0证明数据插入成功,可以插入明细数据
                    if (billId > 0) {
                        List<Retailbill> retailBills = this.createRetailBill(wdtSaleBill.getDetailsList(), billId, wdtSaleBill.getOrderNo(),8);
                        if (CollUtil.isEmpty(retailBills)) {
                            //TODO 手动回滚事物
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
                        for (Retailbill retailbill : retailBills) {
                            retailBillDao.insertSelective(retailbill);
                        }
                        //TODO 插入明细以后,调用修改语句,将总单据的数量改成名字单据的汇总
                        retailbillindexDao.updateBillTotalQty(billId);
                        //TODO 插入资金账户(这里要去取)
                        //根据id 获取当前单据总结
                        double billTotalMoneyByBillId = retailbillindexDao.getBillTotalMoneyByBillId(billId);
                        Retailpayinfo retailpayinfo = new Retailpayinfo();
                        retailpayinfo.setArec(70).setBillid(billId).setOrd(0).
                                setTotal(billTotalMoneyByBillId);
                        retailpayinfoDao.insertSelective(retailpayinfo);
                        //上面生成零售单没有问题,执行将单号插入到过账表中,后面将进行自动过账
                        ZxyNeedauditingretailsbillid zxyNeedauditingretailsbillid = new ZxyNeedauditingretailsbillid();
                        zxyNeedauditingretailsbillid.setRetailBillId(billId);
                        int i = zxyNeedauditingretailsbillidDao.insertSelective(zxyNeedauditingretailsbillid);
                        log.info("插入过账表成功");
                    } else {
                        log.error("零售主单据插入失败" + JSON.toJSONString(retailbillindex));
                        //TODO 手动回滚事物
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                } else {
                    log.error(wdtSaleBill.getOrderNo() + "单据状态为" + wdtSaleBill.getTradeStatus() + "不能执行插入到千方数据库中");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return null;
    }


    @Resource
    private AuditingDao auditingDao;

    @Override
    public void auditingRetailBill(XF_RetailPostPro dto) {
        auditingDao.XF_RetailPost(dto);
    }

    @Override
    public ResultData auditingRetailBillByBillId(int billId) {
        Retailbillindex retailsBillByBillId = retailsBillDao.getRetailsBillByBillId(billId);
        if (BeanUtil.isEmpty(retailsBillByBillId)) {
            return new ResultData(-999, billId + "在零售单中没有找到此单据", null);
        }
        XF_RetailPostPro pro = new XF_RetailPostPro();
        pro.setBillId(retailsBillByBillId.getBillID()).setPosId(retailsBillByBillId.getPosid());
        XF_RetailPostPro xf_retailPostPro = new XF_RetailPostPro();
        try {
            xf_retailPostPro = auditingDao.XF_RetailPost(pro);
        } catch (Exception e) {
            log.error(JSON.toJSONString(pro) + "零售单过账发生异常");
        }
        //无论发生过账发生不发生异常,都需要去检索下主单据的LinkStatus 是否为2,如果为2,证明已经生成零售单汇总,已经过账
        Retailbillindex retailsBillByBillId2 = retailsBillDao.getRetailsBillByBillId(billId);
        //过账成功后将中间库的isAuditing字段改成1
        if (retailsBillByBillId2.getLinkStatus() == 2) {
            ZxyNeedauditingretailsbillid zxyNeedauditingretailsbillid = new ZxyNeedauditingretailsbillid();
            zxyNeedauditingretailsbillid.setRetailBillId(billId);
            int i = zxyNeedauditingretailsbillidDao.updateIsAuditing(zxyNeedauditingretailsbillid);
        }
        log.info("过账返回的值" + JSON.toJSONString(xf_retailPostPro));
        return new ResultData(1, billId + "过账成功", xf_retailPostPro);

    }

    @Override
    public boolean billIfExistByCodeAndPosId(String billCode, int posId) {
        Retailbillindex retailBillIndex = retailsBillDao.billIfExistByCodeAndPosId(billCode, posId);
        if (BeanUtil.isNotEmpty(retailBillIndex)) {
            return true;
        }
        return false;
    }

    @Override
    public ResultData addRetailsBackBillToQianFang(List<WdtSaleBackBillRes.ResponseBean.StockinListBean> retailBackBillList) {
        int erec = 0;
        int ord = 1;
        for (WdtSaleBackBillRes.ResponseBean.StockinListBean stockinListBean : retailBackBillList) {
            String srcOrderNo = stockinListBean.getSrc_order_no();
            int billIndexCountByBillCode = retailbillindexDao.getBillIndexCountByBillCode(srcOrderNo);
            if (billIndexCountByBillCode > 0) {
                log.info("零售退货单" + srcOrderNo + "单号已经存在,不需要在插入到千方中");
                continue;
            }
            Retailbillindex retailbillindex = new Retailbillindex();
            retailbillindex.setBillDate(stockinListBean.getStockin_time()).setBillCode(stockinListBean.getSrc_order_no()).setBillType(152).setComment("旺店通退货自动生成单据").setERec(erec).setKRec(4).setPosid(1).setTotalmoney(Double.parseDouble(stockinListBean.getGoods_amount())).setTotalqty(0).setDiscountMoney(0).setCash(0).setChange(0).setExplain("").setDraft(1).setSaleEtypeid(erec).setBuyerName("");
            retailbillindexDao.insertSelective(retailbillindex);
            int billId = retailbillindex.getBillID();
            for (WdtSaleBackBillRes.ResponseBean.StockinListBean.DetailsListBean detailsListBean : stockinListBean.getDetails_list()) {
                String _ptypeUserCode = detailsListBean.getSpec_no();
                PtypeInfoRes ptypeInfoRes = stocksDao.getPtypeByUserCode(_ptypeUserCode);
                //查询商品编号是否在千方中存在
                if (BeanUtil.isEmpty(ptypeInfoRes)) {
                    retailbillindexDao.deleteIndexByBillId(billId);
                    log.info(detailsListBean.getSpec_no() + "商品编号信息不存在,无法将数据插入到千方中");
                    break;
                }
                //查询库存批号和有效期相关字段
                QfPosGoodsStockQtyRes retailBackGoodsStocksStock = null;
                QfPosGoodsStockQtyRes retailBackGoodsStocksStock1 = null;
                QfPosGoodsStockQtyRes retailBackGoodsStocksStock2 = null;
                int prec = ptypeInfoRes.getRec();
                retailBackGoodsStocksStock1 = stocksDao.getRetailBackGoodsStocks_STOCK(prec, detailsListBean.getBatch_no());
                if (BeanUtil.isEmpty(retailBackGoodsStocksStock1)) {
                    retailBackGoodsStocksStock2 = stocksDao.getRetailBackGoodsStocks_SALE(prec, detailsListBean.getBatch_no());
                    retailBackGoodsStocksStock = retailBackGoodsStocksStock2;
                } else {
                    retailBackGoodsStocksStock = retailBackGoodsStocksStock1;
                }
                if (BeanUtil.isEmpty(retailBackGoodsStocksStock)) {
                    retailbillindexDao.deleteIndexByBillId(billId);
                    log.info(detailsListBean.getSpec_no() + "商品编号信息不是在千方出的库,在千方库存和零售单无法检索到此商品出库批号" + detailsListBean.getBatch_no());
                    break;
                }
                Retailbill retailbill = new Retailbill();
                retailbill
                        .setBillID(billId)
                        .setOrd(ord)
                        .setPRec(ptypeInfoRes.getRec())
                        .setQty(Double.parseDouble(detailsListBean.getGoods_count()))
                        .setAssQty(Double.parseDouble(detailsListBean.getGoods_count()))
                        .setRetailPrice(Double.parseDouble(detailsListBean.getSrc_price()))
                        .setDiscount(100.0)
                        .setDiscountPrice(Double.parseDouble(detailsListBean.getSrc_price()))
                        .setAssDiscountPrice(0.0)
                        .setDiscountTotal(Double.parseDouble(detailsListBean.getSrc_price()) * Double.parseDouble(detailsListBean.getGoods_count()))
                        .setTotal(Double.parseDouble(detailsListBean.getSrc_price()) * Double.parseDouble(detailsListBean.getGoods_count()))
                        .setComment("自动生成单据退货单")
                        .setJobNumber(detailsListBean.getBatch_no())
                        .setOutFactoryDate(retailBackGoodsStocksStock.getOutFactoryDate())
                        .setValidityPeriod(retailBackGoodsStocksStock.getValidityPeriod())
                        .setCostPrice(retailBackGoodsStocksStock.getPrice())
                        .setUnit(1)
                        .setUnitRate(1.0)
                        .setKRec(retailBackGoodsStocksStock.getKRec())
                        .setProviderId(retailBackGoodsStocksStock.getProviderId())
                        .setStockUniqueid(retailBackGoodsStocksStock.getStockUniqueid())
                        .setSaleEtypeid(erec)
                        .setJobCode("")
                        .setGermJobNumber("")
                        .setCheckReportNo("")
                        .setChangeId(ptypeInfoRes.getPtypeChange());
                retailBillDao.insertSelective(retailbill);
                ord = ord + 1;
            }
            ord = 1;

        }

        return null;
    }

    public List<Retailbill> createRetailBill(List<WdtSaleBillRes.ResponseBean.StockoutListBean.DetailsListBean> wdtSaleDetails,
                                             int billId,
                                             String mtBillCode,int _krec) {
        int krec = _krec;
        int erec = 0;
        List<Retailbill> returnList = new ArrayList<>();
        //查询主单据挂的仓库Id
        int ord = 1;
        for (WdtSaleBillRes.ResponseBean.StockoutListBean.DetailsListBean goodsDetailsRe : wdtSaleDetails) {
            for (WdtSaleBillRes.ResponseBean.StockoutListBean.DetailsListBean.PositionListBean positionListBean : goodsDetailsRe.getPositionList()) {
                String _ptypeUserCode = goodsDetailsRe.getSpecNo();
                PtypeInfoRes ptypeInfoRes = stocksDao.getPtypeByUserCode(_ptypeUserCode);
                Gp_BillBatchOrderQuery billBatchOrderQuery = new Gp_BillBatchOrderQuery();
                billBatchOrderQuery.setPrec(ptypeInfoRes.getRec()).setKrec(krec).setUserRec(erec);
                List<QfPosGoodsStockQtyRes> qfPosGoodsStockQtyRes = stocksDao.Gp_BillBatchOrder(billBatchOrderQuery);
                List<QfPosGoodsStockQtyRes> qfPosGoodsStockQtyResNoStopUse = qfPosGoodsStockQtyRes.stream().filter(f -> f.getStopUse() == 0).collect(Collectors.toList());
                //升序排列(将库存进行排序)
                qfPosGoodsStockQtyResNoStopUse = qfPosGoodsStockQtyResNoStopUse.stream().sorted(Comparator.comparing(QfPosGoodsStockQtyRes::getQty)).collect(Collectors.toList());
                if (CollUtil.isEmpty(qfPosGoodsStockQtyResNoStopUse)) {
                    log.error("单据编号:" + mtBillCode + "--" + JSON.toJSONString(_ptypeUserCode) + "商品库存不足" + "销售数量为：" + goodsDetailsRe.getGoodsCount());
                    return null;
                }
                double _rowTotalMoney = Double.parseDouble(goodsDetailsRe.getGoodsCount()) * Double.parseDouble(goodsDetailsRe.getSellPrice());
                //TODO 调用计算库存方法
                List<QfPosGoodsStockQtyRes> createRetailBillStockList = this.getCreateRetailBillStockListByQtyAndJobnumber(qfPosGoodsStockQtyResNoStopUse, Double.parseDouble(positionListBean.getPositionGoodsCount()), positionListBean.getBatchNo()

                );
                if (createRetailBillStockList == null) {
                    return null;
                }
                for (QfPosGoodsStockQtyRes posGoodsStockQtyRes : createRetailBillStockList) {
                    Retailbill retailbill = new Retailbill();
                    retailbill.setBillID(billId).setOrd(ord).setPRec(posGoodsStockQtyRes.getPRec()).setQty(posGoodsStockQtyRes.getRetailBillQty()).setAssQty(posGoodsStockQtyRes.getRetailBillQty()).setRetailPrice(Double.parseDouble(goodsDetailsRe.getSellPrice())).setDiscount(100.0).setDiscountPrice(Double.parseDouble(goodsDetailsRe.getSellPrice())).setAssDiscountPrice(0.0).setDiscountTotal(Double.parseDouble(goodsDetailsRe.getSellPrice()) * posGoodsStockQtyRes.getRetailBillQty()).setTotal(Double.parseDouble(goodsDetailsRe.getSellPrice()) * posGoodsStockQtyRes.getRetailBillQty()).setComment("自动生成单据").setJobNumber(posGoodsStockQtyRes.getJobNumber()).setOutFactoryDate(posGoodsStockQtyRes.getOutFactoryDate()).setValidityPeriod(posGoodsStockQtyRes.getValidityPeriod()).setCostPrice(posGoodsStockQtyRes.getPrice()).setUnit(1).setUnitRate(1.0).setKRec(posGoodsStockQtyRes.getKRec()).setProviderId(posGoodsStockQtyRes.getProviderId()).setStockUniqueid(posGoodsStockQtyRes.getStockUniqueid()).setSaleEtypeid(erec).setJobCode(posGoodsStockQtyRes.getJobcode()).setGermJobNumber(posGoodsStockQtyRes.getGermJobNumber()).setCheckReportNo(posGoodsStockQtyRes.getCheckReportNo()).setChangeId(ptypeInfoRes.getPtypeChange());
                    returnList.add(retailbill);
                    ord++;
                }
            }
        }
        return returnList;
    }

    /**
     * @param stockQtyResList 当前库存剩余数量
     * @param saleQty         当前单据需要销售的数量
     * @return
     */
    public List<QfPosGoodsStockQtyRes> getCreateRetailBillStockList(List<QfPosGoodsStockQtyRes> stockQtyResList, double saleQty) {
        //进来首先判断下可销库存数量是否够,如果不够直接返回null
        double _canSaleStockQty = 0;
        for (int i = 0; i < stockQtyResList.size(); i++) {
            QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes = stockQtyResList.get(i);
            _canSaleStockQty = qfPosGoodsStockQtyRes.getQty() + _canSaleStockQty;
        }
        if (_canSaleStockQty < saleQty) {
            //log.error( "getCreateRetailBillStockList方法"+ "商品库存不足" + "销售数量为：" + saleQty);
            return null;
        }
        //TODO  库存够,执行库存拆分
        List<QfPosGoodsStockQtyRes> resultList = new ArrayList<>();
        double _totalQty = 0;
        double _alreadyQty = 0;//已经赋值数量
        //首先先将所有的 RetailBillQty 赋值为销售的数量
        for (int i = 0; i < stockQtyResList.size(); i++) {
            QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes = stockQtyResList.get(i);
            qfPosGoodsStockQtyRes.setRetailBillQty(saleQty);
            stockQtyResList.set(i, qfPosGoodsStockQtyRes);
        }
        for (int i = 0; i < stockQtyResList.size(); i++) {
            QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes = stockQtyResList.get(i);
            _totalQty = qfPosGoodsStockQtyRes.getQty() + _totalQty;
            //当前库存累加数量大于销售数量时候,需要吧list 返回
            if (_totalQty >= saleQty) {
                //美团的销售数量-已记录数量其实就等于当前库存需要出的数量
                qfPosGoodsStockQtyRes.setRetailBillQty(saleQty - _alreadyQty);
                resultList.add(qfPosGoodsStockQtyRes);
                return resultList;
            } else {
                //需要将当前库存的数量,赋值成生成零售单的数量
                qfPosGoodsStockQtyRes.setRetailBillQty(qfPosGoodsStockQtyRes.getQty());
                resultList.add(qfPosGoodsStockQtyRes);
                _alreadyQty = _alreadyQty + qfPosGoodsStockQtyRes.getQty();
            }
        }
        //log.error(JSON.toJSONString(stockQtyResList) + "商品库存不足" + "销售数量为：" + saleQty);
        return null;
    }

    /**
     * @param stockQtyResList1 当前库存剩余数量
     * @param saleQty          当前单据需要销售的数量
     * @param jobnumber        批次号
     * @return
     */
    public List<QfPosGoodsStockQtyRes> getCreateRetailBillStockListByQtyAndJobnumber(List<QfPosGoodsStockQtyRes> stockQtyResList1, double saleQty, String jobnumber) {
        //TODO  进来，首先进行批号过滤
        List<QfPosGoodsStockQtyRes> stockQtyResList = new ArrayList<>();
        for (QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes : stockQtyResList1) {
            if (qfPosGoodsStockQtyRes.getJobNumber().equals(jobnumber)) {
                stockQtyResList.add(qfPosGoodsStockQtyRes);
            }
        }
        //进来首先判断下可销库存数量是否够,如果不够直接返回null
        double _canSaleStockQty = 0;
        for (int i = 0; i < stockQtyResList.size(); i++) {
            QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes = stockQtyResList.get(i);
            _canSaleStockQty = qfPosGoodsStockQtyRes.getQty() + _canSaleStockQty;
        }
        if (_canSaleStockQty < saleQty) {
            //log.error( "getCreateRetailBillStockList方法"+ "商品库存不足" + "销售数量为：" + saleQty);
            return null;
        }
        //TODO  库存够,执行库存拆分
        List<QfPosGoodsStockQtyRes> resultList = new ArrayList<>();
        double _totalQty = 0;
        double _alreadyQty = 0;//已经赋值数量
        //首先先将所有的 RetailBillQty 赋值为销售的数量
        for (int i = 0; i < stockQtyResList.size(); i++) {
            QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes = stockQtyResList.get(i);
            qfPosGoodsStockQtyRes.setRetailBillQty(saleQty);
            stockQtyResList.set(i, qfPosGoodsStockQtyRes);
        }
        for (int i = 0; i < stockQtyResList.size(); i++) {
            QfPosGoodsStockQtyRes qfPosGoodsStockQtyRes = stockQtyResList.get(i);
            _totalQty = qfPosGoodsStockQtyRes.getQty() + _totalQty;
            //当前库存累加数量大于销售数量时候,需要吧list 返回
            if (_totalQty >= saleQty) {
                //美团的销售数量-已记录数量其实就等于当前库存需要出的数量
                qfPosGoodsStockQtyRes.setRetailBillQty(saleQty - _alreadyQty);
                resultList.add(qfPosGoodsStockQtyRes);
                return resultList;
            } else {
                //需要将当前库存的数量,赋值成生成零售单的数量
                qfPosGoodsStockQtyRes.setRetailBillQty(qfPosGoodsStockQtyRes.getQty());
                resultList.add(qfPosGoodsStockQtyRes);
                _alreadyQty = _alreadyQty + qfPosGoodsStockQtyRes.getQty();
            }
        }
        return null;
    }
}
